package com.learn.springboot3.controller;


import cn.hutool.crypto.asymmetric.RSA;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.security.*;
import java.util.Base64;
import java.util.regex.Pattern;

@RestController
@RequestMapping("system")
public class SystemController {

    @GetMapping("ip")
    public String ipconfig() throws IOException {
        StringBuffer stringBuffer = new StringBuffer();
        ProcessBuilder processBuilder = new ProcessBuilder();
        ProcessBuilder pb = new ProcessBuilder("ipconfig");

        Process process = pb.start();
        String line;
        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));
        while ((line = reader.readLine()) != null) {
            System.out.println(line);

            boolean matches = Pattern.matches("\\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b", line);

            if (matches){
                stringBuffer.append(line);
            }
        }


        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while ((line = errorReader.readLine()) != null) {
            System.out.println("Error: " + line);
            stringBuffer.append(line);
        }

        System.out.println("stringBuffer = " + stringBuffer);

        return stringBuffer.toString();
    }

    @GetMapping("memory")
    public String cpu() throws IOException {
        StringBuffer stringBuffer = new StringBuffer();
        ProcessBuilder pb = new ProcessBuilder("tasklist");

        Process process = pb.start();
        String line;

        BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream(),"GBK"));

        while ((line = reader.readLine()) != null) {



            String trim = line.trim();
            System.out.println(trim);
            stringBuffer.append(line);

        }


        BufferedReader errorReader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
        while ((line = errorReader.readLine()) != null) {
            System.out.println("Error: " + line);
            stringBuffer.append(line+"\n");
        }


        return stringBuffer.toString();
    }

    @GetMapping("rsa")
    public String rsa() throws NoSuchAlgorithmException {
        // 生成RSA密钥对
        KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");
        keyGen.initialize(2048); // 指定密钥大小为2048位
        KeyPair keyPair = keyGen.generateKeyPair();
        PrivateKey privateKey = keyPair.getPrivate();
        PublicKey publicKey = keyPair.getPublic();

        // 使用Base64编码打印私钥
        byte[] privateKeyBytes = privateKey.getEncoded();
        String privateKeyBase64 = Base64.getEncoder().encodeToString(privateKeyBytes);
        System.out.println("Private Key (Base64):");
        System.out.println(privateKeyBase64);

        // 使用Base64编码打印公钥
        byte[] publicKeyBytes = publicKey.getEncoded();
        String publicKeyBase64 = Base64.getEncoder().encodeToString(publicKeyBytes);
        System.out.println("Public Key (Base64):");
        System.out.println(publicKeyBase64);

        // 注意：下面的toString()方法调用仅用于演示，不推荐用于生产环境
        // 因为toString()的输出格式可能不是标准的，并且可能因Java版本而异
        System.out.println("Private Key (toString()):");
        System.out.println(privateKey.toString());

        System.out.println("Public Key (toString()):");
        System.out.println(publicKey.toString());

        return null;
    }
}
